<template>
  <div class="box">
  <h1 class="txt1">添加操作人员</h1>
  <el-form :model="admin" label-width="120px">
      <el-form-item label="账户：">
          <el-input v-model="admin.adminname" />
      </el-form-item>
      <el-form-item label="密码：">
          <el-input type="password" v-model="admin.password" />
      </el-form-item>
      <el-form-item label="请选择角色：">
          <el-radio-group v-model="admin.role">
              <el-radio :label="1">超级管理员</el-radio>
              <el-radio :label="2">普通管理员</el-radio>
          </el-radio-group>
      </el-form-item>
      <p>
        <el-tree :data="data" ref="treeRef" node-key="id" highlight-current show-checkbox />
      </p>
      <el-form-item class="but">
          <el-button type="primary" @click="addAdmin">添加</el-button>
      </el-form-item>
  </el-form>
</div>
</template>
  
<script setup lang="ts">

import { ElMessage } from 'element-plus';
import { reactive,ref } from 'vue';
import service from '../../../untils/service';
import {useRouter} from "vue-router";
import 'element-plus/dist/index.css';
import type Node from 'element-plus/es/components/tree/src/model/node'
import {menudata as data} from "../../../grants/menudata"



const $router = useRouter();

interface IAdmin {
  "adminname": string,
  "password": string,
  "role": number,
  "checkedKeys": string[]
}

// 当前管理员的数据
const admin = reactive<IAdmin>({
  "adminname": "",
  "password": "",
  "role": 1,
  "checkedKeys": []
})
let  treeRef = ref<any>(null);

const addAdmin = ()=>{

  let arr = treeRef.value!.getCheckedKeys();    // 获取的是选中菜单的节点

  let arr1 = treeRef.value!.getHalfCheckedKeys(); // 获取的是半选中菜单

  const Allarr=arr.concat(arr1)




  admin.checkedKeys = Allarr;    


  if (admin.adminname == '' || admin.password == '') {
    ElMessage({
        message:'请输入添加的账号或密码',
        type:'error'
      })
  } else if (admin.adminname !== '' || admin.password !== '') {
    service({
      url:"/admin/add",
      method:"post",
      data:admin
  }).then(res=>{
      if(res.data.code==="200"){
          ElMessage({
              message:"添加成功！",
              type:"success"
          })
          $router.push("/index/operator");  //Addoperator
      }
  })
  }
  
}
//  权限

</script>
  
<style scoped>
.txt1 {
  text-align: center;
}

.box {
  width: 500px;
  height: auto;
  margin: auto;
 
}


</style>